﻿var item;

function item_init() {
    item = [];
}

function item_update() {
    if (player.y < HEIGHT * 0.25)
        item_all_follow();

    var i = item.length;
    while (i--) {
        item[i].update();
        if (item[i].y > HEIGHT + 20 || item[i].y < -20) {
            item_remove(i);
            continue;
        }
        if (item[i].collide() == 2) {
            item_remove(i);
            continue;
        }
    }
}

function item_all_follow() {
    var i = item.length;
    while (i--) {
        item[i].state = 2;
        item[i].update = item_follow_fast;
    }
}

function item_draw() {
    var i = item.length;
    while (i--) {
        item[i].draw();
    }
}

function item_remove(i) {
    item.splice(i, 1);
}

function item_collide() {
    var xx = abs(this.x - player.x);
    var yy = abs(this.y - player.y);
    if (xx > 75 || yy > 75)
        return 0;
        
    if (xx < 16 && yy < 16) {
        this.oncollide();
        return 2;
    } else if (player.shift && this.state == 0) {
        this.state = 1;
        this.update = item_follow_slow;
    }
    return 0;
}

function item_fall() {
    this.y += this.vy;
    if (this.vy < 2)
        this.vy += 0.05;
}

function item_follow_slow() {
    var ang = patan2(this.x, this.y);
    this.x += 4 * cos(ang);
    this.y += 4 * sin(ang);
}

function item_follow_fast() {
    var ang = patan2(this.x, this.y);
    this.x += 7 * cos(ang);
    this.y += 7 * sin(ang);
}

function powerup() {
    player_powerup(this.power);
}

function item_0(x, y) {
    item[item.length] = {x: x, y: y, vx: 0, vy: -3.5,
                        power: 0.05,
                        state: 0,
                        update: item_fall,
                        draw: item_draw_0,
                        collide: item_collide, oncollide: powerup};
}

function item_1(x, y) {
    item[item.length] = {x: x, y: y, vx: 0, vy: -3.5,
                        power: 0.2,
                        state: 0,
                        update: item_fall,
                        draw: item_draw_1,
                        collide: item_collide, oncollide: powerup};
}
